package com.iweb.io;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class IoServer {
    public static void main(String[] args) throws Exception {
        ServerSocket server = new ServerSocket(9000);
        try {
            while (true) {
                Socket socket = server.accept(); // 阻塞接收
                new Thread(() -> {
                    try {
                        InputStream in = socket.getInputStream();
                        byte[] buf = new byte[1024];
                        String name = Thread.currentThread().getName();
                        while (true) {
                            int len = 0;
                            while (true) {
                                len = in.read(buf);
                                if (len == -1) break;
                                System.out.printf("线程：%s读取数据%s\n", name, new String(buf, 0, len));
                            }
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        } finally {
            server.close();
        }
    }
}
